package util.thread.creatthread;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 李栋
 * @version 1.0.0
 * @className ThreadDemoTest.java
 * @createTime 2021年09月06日 20:55:00
 */
public class ThreadDemoTest {


    private static final Logger logger = LoggerFactory.getLogger(ThreadDemoTest.class);

    /**
     * 调用 start 方法方可启动线程，而 run 方法只是 thread 的一个普通方法调用，还是在主线程里执行
     */
    @Test
    public void test() throws InterruptedException {
        DemoOneThread demoOneThread = new DemoOneThread();
        // 调用 run() 会打印出主线程的信息
        demoOneThread.run();

        // 调用 start() 会打印出新启的线程的信息
        new Thread(demoOneThread).start();

        Thread.sleep(2000);
        Thread thread = Thread.currentThread();
        logger.info("当前线程信息：{}-{}", thread.getId(), thread.getName());
    }

    class DemoOneThread implements Runnable {

        private final Logger logger = LoggerFactory.getLogger(DemoOneThread.class);

        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            logger.info("当前线程信息：{}-{}", thread.getId(), thread.getName());
        }
    }

}

